package com.tanhua.server.controller;

import com.tanhua.model.vo.CountsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.BlackListService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import com.tanhua.server.service.UserSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @auther : 白月尘
 * @date : 11:25  2021/10/17
 */
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserSetService userSetService;
    @Autowired
    private BlackListService blackListService;


    @GetMapping
    public UserInfoVo findById(Long userId) {
        return userInfoService.findById(userId);
    }

    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        String mobile = UserHolder.getMobile();
        userService.sendMsg(mobile);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/phone/checkVerificationCode")
    public Map<String, Boolean> checkVerificationCode(@RequestBody Map map) {
        String verificationCode = map.get("verificationCode").toString();
        boolean verification = userService.checkVerificationCode(verificationCode);
        System.out.println(verification);
        Map<String, Boolean> booleanMap = new HashMap<>();
        booleanMap.put("verification", verification);
        System.out.println(map.get("verification"));
        return booleanMap;
    }

    @GetMapping("/settings")
    public ResponseEntity settings() {
        SettingsVo settingsVo = userSetService.findById();
        return ResponseEntity.ok(settingsVo);
    }

    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        userSetService.setQuestion(map.get("content").toString());
        return ResponseEntity.ok(null);
    }

    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map) {
        userSetService.setSetting(map);
        return ResponseEntity.ok(null);
    }

    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") long uid) {
        blackListService.deleteBlackList(uid);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/blacklist")
    public PageResult blacklist(@RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "10") int pagesize) {
        return blackListService.blacklist(page, pagesize);
    }

    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map) {
        String phone = map.get("phone").toString();
        userService.setPhone(phone);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@PathVariable("type") int type,
                                  @RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "10") int pagesize) {
        PageResult pageResult=userService.findfriends(type,page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**互相喜欢，喜欢，粉丝 - 统计
     * GET/users/counts
     * eachLoveCount  互相喜欢	loveCount 喜欢  fanCount  fanCount 粉丝
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        CountsVo vo = userService.counts();
        return ResponseEntity.ok(vo);
    }
    /**粉丝 - 喜欢
     * POST/users/fans/:uid
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fans(@PathVariable("uid") Long likeUserId){
        userService.fans(likeUserId);
        return ResponseEntity.ok(null);
    }
    /**喜欢 - 取消
     * DELETE/users/like/:uid
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity like(@PathVariable("uid") Long likeUserId){
        userService.unlike(likeUserId);
        return ResponseEntity.ok(null);
    }
}
